<template>
  <div class="container">
    <div class="left"></div>
    <div class="right">
      <div class="login-panel">
        <div class="login-title">Star云盘</div>
        <el-form>
          <el-form-item >
            <el-input
              v-model="loginRequest.account"
              placeholder="请输入账号"
              prefix-icon="User"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item v-if="action === 'signup'">
            <div class="email-captcha">
              <el-input
                v-model="loginRequest.email"
                placeholder="请输入邮箱"
                prefix-icon="Message"
                clearable
              ></el-input>
              <el-button type="primary" @click="getCaptcha" :disabled="captchaTime !== 0"
                >{{captchaTime === 0? '获取验证码': captchaTime+ '秒后重发'}}</el-button
              >
            </div>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="loginRequest.password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item v-if="action === 'signup'">
            <el-input
              v-model="loginRequest.captcha"
              placeholder="请输入验证码"
              prefix-icon="Key"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox
              v-model="loginRequest.rememberMe"
              label="记住我"
            ></el-checkbox>
          </el-form-item>
        </el-form>
        <div class="login-other">
          <el-button @click="forgetPassword" type="text">记住密码?</el-button>
          <el-button @click="handleNoAccount" type="text" v-if="action === 'signin'"> 没有账号?</el-button>
          <el-button @click="handleLogin" type="text" v-if="action === 'signup'">去登录</el-button>
        </div>
        <el-button class="login-button" type="primary" @click="login"
          >{{action === 'signin'? '登录': '注册'}}</el-button
        >
      </div>
    </div>

    <CaptchaDialog ref="captchaDialog"></CaptchaDialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import CaptchaDialog from "@/views/login/CaptchaDialog.vue";
import { ElMessage } from "element-plus";
import { LoginRequest } from "@/type";

const loginRequest = ref<LoginRequest>({
  account: "",
  password: "",
  captcha: "",
  rememberMe: false,
  email: "",
});

const captchaDialog = ref();
let action = ref("signin")

const forgetPassword = function () {
  console.info("forgetPassword");
};
const handleNoAccount = function () {
  action.value = "signup";
};
const handleLogin = function () {
  action.value = "signin";
};

const login = function () {
  console.log("login");
};

const captchaTime = ref(0)
const getCaptcha = () => {
  if (!loginRequest.value.email) {
    ElMessage.warning("请输入邮箱");
    return;
  }
  // captchaTime.value = 60
  // let id = setInterval(() => {
  //   captchaTime.value--
  //   if (captchaTime.value === 0) {
  //     clearInterval(id)
  //   }
  // }, 1000);
  captchaDialog.value.showDialog(loginRequest.value.email);
};
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  background: url("@/assets/background.jpg");
  display: flex;

  .left {
    background-size: 800px;
    background-position: center;
    background-image: url("@/assets/login_img-db08ed45.png");
    background-repeat: no-repeat;
    flex: 1;
  }

  .right {
    width: 700px;
    display: flex;
    align-items: center;

    .login-panel {
      width: 360px;
      background: #ffffff;
      border-radius: 5px;
      padding: 25px;

      .el-form-item {
        display: flex;
        &:last-child {
          margin-bottom: 0;
        }
      }

      .login-title {
        text-align: center;
        font-weight: 700;
        margin-bottom: 20px;
        font-size: 18px;
      }

      .login-other {
        display: flex;
        justify-content: space-between;
      }

      .login-button {
        width: 100%;
      }

      .email-captcha {
        display: flex;

        .el-button {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
